package com.yangfan.cloudalibaba.service.impl;

import com.yangfan.cloudalibaba.dao.OrderDao;
import com.yangfan.cloudalibaba.domain.Order;
import com.yangfan.cloudalibaba.service.AccountService;
import com.yangfan.cloudalibaba.service.OrderService;
import com.yangfan.cloudalibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Autowired
    private StorageService storageService;
    @Autowired
    private AccountService accountService;


    @Override
    @GlobalTransactional(name = "my_test_tx_group",rollbackFor = Exception.class,timeoutMills = 60000)
    public void create(Order order) {

        log.info(" ------------------> 生成订单");
        orderDao.create(order);
        log.info(" ------------------> 生成订单完毕");


        log.info(" ------------------> 开始扣减库存");
        storageService.decreaseStorage(order.getProductId(),order.getCount());
        log.info(" ------------------> 结束扣减库存");


        log.info(" ------------------> 开始扣减余额");
        accountService.decreaseAccount(order.getUserId(),order.getMoney());
        log.info(" ------------------> 结束扣减余额");


        log.info(" ------------------> 开始修改订单状态");
        orderDao.update(order.getUserId(),1);
        log.info(" ------------------> 结束修改订单状态");


        log.info(" ------------------> 全部成功 success !!");

    }


}
